package com.example.graduatetest.model;

import android.util.Log;

import com.example.graduatetest.bean.Classes;
import com.example.graduatetest.bean.Student;
import com.example.graduatetest.bean.Teacher;
import com.example.graduatetest.callback.ClassCallBack;
import com.example.graduatetest.callback.ClassList;
import com.example.graduatetest.callback.PostCallback;
import com.example.graduatetest.callback.StringCallBack;
import com.example.graduatetest.callback.StudentList;
import com.example.graduatetest.utils.Constant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class ClassModel {
    private static String www = Constant.www;
    private static String url = www+"/class/selectClasses?number=";
    private static String createClassUrl = www+"/class/createClass";
    private static String addClassUrl = www+"/class/addClass";
    private static String showStudentsUrl = www+"/student/selectStudentList";
    private static String getOneClassUrl = www+"/class/getOneClass";
    private static String getClassListTeacherUrl = www+"/class/selectClassListTeacher";
    //得到选择这门课的学生列表
    public static void getStudentList(final String id, final StudentList callback){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder = HttpUrl.parse(showStudentsUrl)
                        .newBuilder();
                urlBuilder.addQueryParameter("id", id);
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJsonStudent(jsonData, callback);
                } catch (IOException e) {
                    callback.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }
    //获得某一Class对象
    public static void getOneClass(final String id, final ClassCallBack callback){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder = HttpUrl.parse(getOneClassUrl)
                        .newBuilder();
                urlBuilder.addQueryParameter("id", id);
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJSONClasses(jsonData, callback);
                } catch (IOException e) {
                    callback.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //解析Student数组
    private static void parseJsonStudent(String jsonData,StudentList callback){
        try{
            JSONArray jsonArray = new JSONArray(jsonData);
            Gson gson = new Gson();//实例化gson
            //解析Classes对象
            Log.e("haha", jsonArray.toString());
            List<Student> studentList = gson.fromJson(jsonArray.toString(), new TypeToken<List<Student>>(){}.getType());
            callback.onSuccess(studentList);
        }catch (Exception e){
            callback.onFailed();
            e.printStackTrace();
        }
    }


    //得到课程列表
    public static void getClassList(final String studentNumber, final ClassList callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url+studentNumber)
//                        .header("number", studentNumber)
                        .build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJSON(jsonData,callBack);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }
    public static void getClassListTeacher(final String teacher_number, final ClassList callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                OkHttpClient okHttpClient = new OkHttpClient();
                Request.Builder reqBuild = new Request.Builder();
                HttpUrl.Builder urlBuilder = HttpUrl.parse(getClassListTeacherUrl)
                        .newBuilder();
                urlBuilder.addQueryParameter("teacher_number", teacher_number);
                reqBuild.url(urlBuilder.build());
                Request request = reqBuild.build();
                Call call = okHttpClient.newCall(request);
                try {
                    Response response = call.execute();
                    String jsonData = response.body().string();
                    parseJSON(jsonData,callBack);
                } catch (IOException e) {
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }
    //创建课程请求服务器方法
    public static void createClass(final String name,final String teacher,final String time,final String color,final String teacher_number, final PostCallback callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                    RequestBody body = new FormBody.Builder()
                            .add("name", name)
                            .add("teacher", teacher)
                            .add("time", time)
                            .add("color",color)
                            .add("teacher_number", teacher_number)
                            .build();
                    Log.e("haha", color);
                    Request request = new Request.Builder()
                            .url(createClassUrl)
                            .post(body)
                            .build();//创建一个Request对象

                    Response response = client.newCall(request).execute();//发送请求获取返回数据
                    String responseData = response.body().string();//处理返回的数据
                    Log.e("haha", responseData);
                    callBack.onSuccess();

                }catch (Exception e){
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();

    }

    //加入课程 方法
    public static void addClass(final String id,final String studentNumber,final StringCallBack callBack){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try{
                    OkHttpClient client = new OkHttpClient();//新建一个OKHttp的对象
                    RequestBody body = new FormBody.Builder()
                            .add("id", id)
                            .add("number", studentNumber)
                            .build();
                    Log.e("haha", id);
                    Request request = new Request.Builder()
                            .url(addClassUrl)
                            .post(body)
                            .build();//创建一个Request对象

                    Response response = client.newCall(request).execute();//发送请求获取返回数据
                    String responseData = response.body().string();//处理返回的数据
                    Log.e("haha", responseData);
                    callBack.onSuccess(responseData);

                }catch (Exception e){
                    callBack.onFailed();
                    e.printStackTrace();
                }
            }
        }).start();
    }

    //解析json对象
    private static void parseJSON(String jsonData,ClassList callBack){
        try{
            JSONArray jsonArray = new JSONArray(jsonData);
            Gson gson = new Gson();//实例化gson
            //解析Classes对象
            Log.e("haha", jsonArray.toString());
            List<Classes> classesList = gson.fromJson(jsonArray.toString(), new TypeToken<List<Classes>>(){}.getType());
            callBack.onSuccess(classesList);
        }catch (Exception e){
            callBack.onFailed();
            e.printStackTrace();
        }
    }

    //解析Classes对象
    private static void parseJSONClasses(String jsonData,ClassCallBack callBack){
        try{
            JSONObject jsonObject = new JSONObject(jsonData);
            Gson gson = new Gson();//实例化gson
            //解析Classes对象
            Log.e("haha", jsonObject.toString());
            Classes classes = gson.fromJson(jsonObject.toString(), Classes.class);
            callBack.onSuccess(classes);
        }catch (Exception e){
            callBack.onFailed();
            e.printStackTrace();
        }
    }
}
